
const requsts = require("/util/requst");
const util = require("/util/util");
Page({
  data: {
    name:"",
    tell:"",
    addres:"",
    checked:false,
    clear:true,
    shippingaddress:"",  //收货地址
    detailedaddress:"",   //详细地址
    userId:"", //用户Id
    shippingId:"",
  },
  onItemInput(e){
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  onClear(e){
       this.setData({
      [e.target.dataset.field]: '',
    });
  },
  //地图
  onExtraTap(){
    var that = this
    my.chooseLocation({
         success:(res)=>{
          console.log(JSON.stringify(res))
          //my.alert({content:JSON.stringify(res)})
          //my.alert({content:JSON.stringify(res.provinceName+res.cityName+res.adName)})
          if(res.provinceName==undefined&&res.cityName==undefined&&res.adName==undefined){
            that.setData({
              detailedaddress:res.address
            })
          }else{
            that.setData({
              shippingaddress:(res.provinceName+res.cityName+res.adName),
              detailedaddress:res.address
            })
          }
          
        },
        fail:(error)=>{
          //my.alert({content: '调用失败：'+JSON.stringify(error), });
        },
    });
  },
  submit(){
     if(!this.data.name){
       util.toastfn("请填写收货人姓名")
       return
     }
     if(!this.data.tell){
       util.toastfn("请输入手机号码")
       return
     }
     if(!this.data.shippingaddress){
       util.toastfn("请填写收货地址")
       return
     }
     if(!this.data.detailedaddress){
       util.toastfn("请填写详细地址")
       return
     }
     var param={
          userId:this.data.userId,
          shippingName:this.data.name,
          shippingPhone:this.data.tell,
          shippingAddress:this.data.shippingaddress,
          detailedAddress:this.data.detailedaddress,
          shippingId:this.data.shippingId,
          type:1,
          isDefault:this.data.checked
        }
     var that=this
     my.request({
        url: requsts.url+"/user/operatorShippingAddress",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
              return
          }
          my.navigateTo({
            url: '/page/common/myaddress/myaddress',
          });
        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
      
  },
  onBlur(e){
      this.setData({
        detailedaddress:e.detail.value
      })
      if(!this.data.detailedaddress){
       util.toastfn("请填写详细地址")
       return
     }
  }, 
  switch2Change(e){
     console.log('switch2 发生 change 事件，携带值为', e.detail.value);
     if(e.detail.value){
        this.setData({
        checked:1
        })
     }else{
        this.setData({
          checked:0
        })
     }
     
     console.log(this.data.checked)
  },
  onLoad(e){
      var that=this
      my.getStorage({
        key: 'userId', // 缓存数据的key
        success: (res) => {
          that.setData({
            userId:res.data.userId,
            shippingId:e.id
          })
          that.init()
        },
      });
  },
  init(){
    var param={
      userId:this.data.userId,
      shippingId:this.data.shippingId
    }
    var that=this
      my.request({
        url: requsts.url+"/user/getShippingAddressDetail",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        data:param,
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
              return
          }
          that.setData({
            name:res.data.data.shippingName,
            tell:res.data.data.shippingPhone,
            shippingaddress:res.data.data.shippingAddress,
            detailedaddress:res.data.data.detailedAddress,
          })
        },
        fail: function(res) {
            util.toastfn(res.errorMessage)
        },
      });
  },
  deletedadd(){
    my.confirm({
      title: '温馨提示',
      content: '您确定要删除该地址吗？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if(result.confirm==false){
            return
        }
        var param={
          userId:this.data.userId,
          shippingId:this.data.shippingId,
          type:2,
        }
        console.log(param)
        var that=this
        my.request({
          url: requsts.url+"/user/operatorShippingAddress",
          method: 'POST',
          headers: {'content-type': 'application/x-www-form-urlencoded'},
          data:param,
          dataType: 'json',
          success: function(res) {
            console.log(res)
            if(res.data.code!=0){
                util.toastfn(res.data.msg)
            }else{
                my.navigateTo({
                  url: '/page/common/myaddress/myaddress',
                });
            }
          },
          fail: function(res) {
              util.toastfn(res.errorMessage)
          },
        });
      },
    });
  }
});